package com.salon.server.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.salon.server.entity.Sale;
import com.salon.server.model.R;
import com.salon.server.model.request.SaleRequest;
import com.salon.server.service.SaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 销售记录表 前端控制器
 * </p>
 *
 * @author zooy
 * @since 2025-08-28
 */
@RestController
@RequestMapping("salon/sale")
public class SaleController {

    @Autowired
    private SaleService saleService;

    /**
     * page
     */
    @PostMapping("/page")
    public R page(@RequestBody SaleRequest request) {
        return R.success(saleService.page(request, new LambdaQueryWrapper<Sale>()
                .eq(StrUtil.isNotEmpty(request.getMemberName()), Sale::getMemberName, request.getMemberName())
                .eq(StrUtil.isNotEmpty(request.getProductName()), Sale::getProductName, request.getProductName())
                .eq(StrUtil.isNotEmpty(request.getPaymentMethod()), Sale::getPaymentMethod, request.getPaymentMethod())
                .between(request.getDateRange() != null && request.getDateRange().size() == 2,
                        Sale::getCreatedAt, request.getDateRange().get(0).atStartOfDay(), request.getDateRange().get(1).atTime(23, 59, 59))
        ));
    }

     /**
     * add
      * 购买产品
     */
    @PostMapping("/purchase")
    public R add(@RequestBody Sale sale) {

        return R.success(saleService.saveOrUpdate(sale));
    }

    /**
     * 我的购买
     */
    @PostMapping("/myPurchases")
    public R myPurchase(@RequestBody SaleRequest request) {

        return R.success(saleService.page(request, new LambdaQueryWrapper<Sale>().eq(Sale::getMemberId, request.getMemberId())));
    }

}
